package com.intel.factorytest.activity;

import java.util.ArrayList;
import java.util.List;

import com.intel.factorytest.R;

import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;

public class TPTest extends BaseActivity implements OnTouchListener {

    private static final String TAG = "TPTest";
    private static int RECT_HIGHT = 50;
    private static int RECT_HIGHT_SPACE = 5;
    private static int RECT_WIDTH = 20;
    private static int RECT_WIDTHT_SPACE = 20;;

    private DisplayMetrics metric;
    private Paint mRedPaint;
    private Paint mTouchPaint;

    private ImageView mImageView;
    private Bitmap mBitmap;
    private Canvas mCanvas;

    private List<Rect> mTestRectList = new ArrayList<Rect>();
    
    private List<Rect> mTouchRectList = new ArrayList<Rect>();

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.tptest);
        mImageView = (ImageView) findViewById(R.id.myrect);
        initView();

        metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);

        orientation = getResources().getConfiguration().orientation;

        mBitmap = Bitmap.createBitmap(metric.widthPixels, metric.heightPixels,
                Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);

        mRedPaint = new Paint();
        mRedPaint.setAntiAlias(true);
        mRedPaint.setColor(Color.RED);
        mRedPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mRedPaint.setStrokeWidth(2);

        mTouchPaint = new Paint();
        mTouchPaint.setAntiAlias(true);
        mTouchPaint.setColor(Color.GREEN);
        mTouchPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mTouchPaint.setStrokeWidth(2);

//        int iRectHRight[] = { RECT_HIGHT,
//                metric.widthPixels / 2 + (RECT_WIDTHT_SPACE + RECT_WIDTH) / 2,
//                metric.widthPixels };

        int iRectHTop[] = { metric.heightPixels,
                metric.heightPixels / 2 + (RECT_HIGHT + RECT_HIGHT_SPACE) / 2,
                RECT_HIGHT };

        for (int i = 0; i < iRectHTop.length; i++) {
            for (int iRight = RECT_WIDTH; iRight < metric.widthPixels; iRight += (RECT_WIDTHT_SPACE + RECT_WIDTH)) {
                Rect mRect = new Rect(iRight - RECT_WIDTH, iRectHTop[i],
                        iRight, iRectHTop[i] - RECT_HIGHT);
                mTestRectList.add(mRect);
            }
        }
        
//        
//        int iRectHRight[] = { metric.widthPixels,
//                metric.widthPixels / 2 + (RECT_HIGHT + RECT_HIGHT_SPACE) / 2,
//                RECT_HIGHT };
//
//        for (int i = 0; i < iRectHRight.length; i++) {
//            for (int iTop = RECT_WIDTH; iTop < metric.heightPixels; iTop += (RECT_WIDTHT_SPACE + RECT_WIDTH)) {
//                Rect mRect = new Rect(iRectHRight[i] - RECT_HIGHT, iTop,
//                        iRectHRight[i], iTop - RECT_WIDTH);
//                mVerticalRectList.add(mRect);
//            }
//        }

//        int iRectVLeft[] = { 0,
//                metric.widthPixels / 2 - (RECT_HIGHT + RECT_HIGHT_SPACE) / 2,
//                metric.widthPixels - RECT_HIGHT };
//
//        // for (int i=0;i<iRectVLeft.length;i++)
//        {
//            for (int iTop = RECT_HIGHT+RECT_WIDTHT_SPACE ; iTop < (iRectHTop[1]-RECT_HIGHT); iTop += (RECT_WIDTHT_SPACE + RECT_WIDTH)) {
//                Rect mRect = new Rect(0, iTop, RECT_HIGHT, iTop - RECT_WIDTH);
//                mTestRectList.add(mRect);
//            }
//        }

        for (Rect rect : mTestRectList) {
            mCanvas.drawRect(rect, mRedPaint);
        }
        mImageView.setImageBitmap(mBitmap);
        mImageView.setOnTouchListener(this);
        // mImageView.setFocusable(true);
        // mImageView.postInvalidate();
    }


    public void setIs_pass(boolean is_pass) {
        this.is_pass = is_pass;
    }

    protected void initView() {
        mSucBtn = (Button) findViewById(R.id.success_button);
        mSucBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = true;
                finish();
            }
        });
        attachOnTouchListener(mSucBtn);

        mFailBtn = (Button) findViewById(R.id.fail_button);
        mFailBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                is_pass = false;
                finish();
            }
        });
        attachOnTouchListener(mFailBtn);

        mRepeatBtn = (Button) findViewById(R.id.repeat_button);
        mRepeatBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // PointerLocation.this.recreate();
                recreate();
            }
        });
        attachOnTouchListener(mRepeatBtn);
    }

    @Override
    public void finish() {
        // TODO Auto-generated method stub
        tc.setTestResult(is_pass);
        super.finish();
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        mBitmap.recycle();
        super.onDestroy();
    }

    private boolean isInRect(int x, int y, Rect mRect) {
        if ((x >= mRect.left) && (x <= mRect.right) && (y >= mRect.bottom)
                && (y <= mRect.top)) {
            return true;
        }
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN
                || action == MotionEvent.ACTION_MOVE) {
            // Log.d(TAG, "onTouch X:" + event.getX() + "   Y:" + event.getY());
            int pointX = (int) event.getX();
            int pointY = (int) event.getY();
            boolean bRedraw = false;

                for (Rect rect : mTestRectList) {
                    if (isInRect(pointX, pointY, rect)) {
                        if (!mTouchRectList.contains(rect)){
                            mTouchRectList.add(rect);
                            mCanvas.drawRect(rect, mTouchPaint);
                            bRedraw = true;
                        }                    
                    }
                }
                
                if (mTouchRectList.size()==mTestRectList.size()){
                    setIs_pass(true);
                    finish();
                }

            if (bRedraw) {
                mImageView.postInvalidate();
            }

            // int coordsX = (int) event.getX();
            // int coordsY = (int) event.getY();
            // mCanvas.drawPoint(coordsX, coordsY, mRedPaint);
            // // mCanvas.drawRect(tmpRect, mGreenPaint);
            // // mImageView.setImageBitmap(mBitmap);
            // mImageView.postInvalidate();
        }
        return true;
    }

}
